﻿<!DOCTYPE HTML>
<html  xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head th:include="_meta :: header">
<meta charset="utf-8">
<title>管理员列表</title>
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">权限配置</a>
            <a>
              <cite>用户管理</cite></a>
          </span>
	<a class="layui-btn layui-btn-small" style="float: right;height: 30px;line-height: 30px;margin-top: 5px;" onclick="location.reload()" title="刷新">
		<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>

<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body ">
					<form  class="layui-form layui-col-space5" action="#" th:action="@{/user}"  method="post">

						<div class="layui-inline ">
							<input type="text" class="layui-input" style="width:250px" placeholder="输入管理员名称" id="" th:value="${name}" name="name"/>
						</div>
						<div class="layui-inline layui-show-xs-block">
							<button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
						</div>
						<div class="cl pd-5 bg-1 bk-gray " >
							<span class="l">
								<a sec:authorize="hasAuthority('/user/delete-user')"  href="javascript:;" th:onclick="'javascript:deleteAll(\''+@{/user/deleteAll-user}+'\')'"  class=" layui-btn layui-btn-danger "><i class="layui-icon">&#xe64d;</i> 批量删除</a>
								<a sec:authorize="hasAuthority('/user/add-user')" href="javascript:;" onclick="admin_add('添加管理员','/user/add-user',800,500)" class="layui-btn layui-btn-normal "><i class="layui-icon">&#xe600;</i> 添加管理员</a>
							</span>
							<span class="r wei-height" style="float: right;margin-right: 20px">共有数据：<strong
									th:text="${pageInfo.total}"></strong> 条</span>
						</div>
						<div class="layui-card-body" style="text-align: center">
							<table class="layui-table layui-form">
								<thead>
								<tr>
									<th scope="col" colspan="10">管理员列表</th>
								</tr>
								<tr class="text-c">
									<th width="25"><input type="checkbox" name="" value="" lay-filter="checkall" lay-skin="primary"/></th>
									<th width="40">ID</th>
									<th width="150">管理员</th>
									<th width="40">性别</th>
									<th width="150">手机</th>
									<th width="180">邮箱</th>
									<th>角色</th>
									<th width="130">加入时间</th>
									<th width="100">是否已启用</th>
									<th width="100">操作</th>
								</tr>
								</thead>
								<tbody>
								<tr class="text-c" th:if="${pageInfo.list.size() == 0}"><td colspan="15"><strong>暂无数据</strong></td></tr>
								<tr class="text-c" th:each="user:${pageInfo.list}">
									<td><input type="checkbox" value="1" th:value="${user.id}" name="id" lay-skin="primary"/></td>
									<td th:text="${user.id}"></td>
									<td th:text="${user.username}"></td>
									<td th:text="${user.sexType ==  1} ? '男' : '女'"></td>
									<td th:text="${user.tel}"></td>
									<td th:text="${user.email}"></td>
									<td >
										<span th:each="role:${user.roles}" th:text="${role.name}"></span>
									</td>
									<td th:text="${#calendars.format(user.createtime,'yyyy-MM-dd')}"></td>
									<td class="td-status" th:if="${user.available}"><span class="label label-success radius" >已启用</span></td>
									<td class="td-status" th:unless="${user.available}"><span class="label radius">已停用</span></td>
									<td class="td-manage">

										<a title="停用" sec:authorize="hasAuthority('/user/available-user')"  style="text-decoration:none" th:if="${user.available and user.id != 1}" th:onclick="'javascript:admin_stop(this,\'user'+@{/available-user}+'/'+${user.id}+'\')'" href="javascript:;"><i class="layui-icon">&#xe651;</i></a>
										<a title="启用" th:if="${!user.available and user.id != 1}" sec:authorize="hasAuthority('/user/available-user')"  style="text-decoration:none" th:unless="${user.available}" th:onclick="'javascript:admin_start(this,\'user'+@{/available-user}+'/'+${user.id}+'\')'" href="javascript:;"><i class="layui-icon">&#xe652;</i></a>

										<a title="修改密码" sec:authorize="hasAuthority('/user/modify-password')"  href="javascript:;" th:onclick="'javascript:admin_edit(\'修改管理员密码\',\'user'+@{/modify-password}+'/'+${user.id}+'\',800,400)'" class="ml-5" style="text-decoration:none"><i class="layui-icon">&#xe716;</i></a>
										<a title="编辑信息" sec:authorize="hasAuthority('/user/edit-user')"  href="javascript:;" th:onclick="'javascript:admin_edit(\'编辑管理员信息\',\'user'+@{/edit-user}+'/'+${user.id}+'\',800,400)'" class="ml-5" style="text-decoration:none"><i class="layui-icon">&#xe642;</i></a>
										<a title="删除" th:if="${user.id != 1}"    sec:authorize="hasAuthority('/user/delete-user')"  href="javascript:;" th:onclick="'javascript:admin_del(this,\'user'+@{/delete-user}+'/'+${user.id}+'\')'" class="ml-5" style="text-decoration:none"><i class="layui-icon">&#xe640;</i></a>

									</td>
								</tr>
								</tbody>
							</table>
						</div>
						<div th:include="_pagination :: page"></div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<!--_footer 作为公共模版分离出去-->
<div th:replace="_footer :: footerjs">
</div>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" th:src="@{/lib/My97DatePicker/4.8/WdatePicker.js}" src="lib/My97DatePicker/4.8/WdatePicker.js"></script> 
<script type="text/javascript" th:src="@{/lib/datatables/1.10.0/jquery.dataTables.min.js}" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script> 
<script type="text/javascript" th:src="@{/lib/laypage/1.2/laypage.js}" src="lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript">

	layui.use(['laydate','form'], function(){
		var laydate = layui.laydate;
		var form = layui.form;
		// 监听全选
		form.on('checkbox(checkall)', function(data){

			if(data.elem.checked){
				$('tbody input').prop('checked',true);
			}else{
				$('tbody input').prop('checked',false);
			}
			form.render('checkbox');
		});
		form.on('select(selectchange)', function(data){
			console.log(data.value); //得到被选中的值
			var pageNo = $('#pageNo');
			var Form = $('form')
			pageNo.val(1);
			Form.submit();
			form.render('select');
		});
	});



	/*
        参数解释：
        title	标题
        url		请求的url
        id		需要操作的数据id
        w		弹出层宽度（缺省调默认值）
        h		弹出层高度（缺省调默认值）
    */
/*管理员-增加*/
function admin_add(title,url,w,h){
	layer_show(title,url,w,h);
}

/*管理员-编辑*/
function admin_edit(title,url,w,h){
	layer_show(title,url,w,h);
}
/*管理员-删除*/
function admin_del(obj,url){
	layer.confirm('确认要删除吗？',function(index){
		$.ajax({
			type: 'POST',
			url: url,
			dataType: 'json',
			success: function(data){
				if(data == 1){
					window.location.reload();
				}else{
					layer.msg('删除失败!',{icon:5,time:1000});
				}
			},
			error:function(data) {
				console.log(data.msg);
			},
		});		
	});
}

/*管理员-停用*/
function admin_stop(obj,url){
	layer.confirm('确认要停用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		$.ajax({
			type: 'POST',
			url: url,
			dataType: 'json',
			success: function(data){
				if(data == 1){
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_start(this,\''+url+'\')" href="javascript:;" title="启用" style="text-decoration:none"><i class="Hui-iconfont">&#xe615;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label radius">已停用</span>');
					$(obj).remove();
					layer.msg('已停用!',{icon: 6,time:1000});
				}else{
					layer.msg('启用失败!',{icon:5,time:1000});
				}
			},
			error:function(data) {
				console.log(data.msg);
			},
		});
	});
}

/*管理员-启用*/
function admin_start(obj,url){
	layer.confirm('确认要启用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		$.ajax({
			type: 'POST',
			url: url,
			dataType: 'json',
			success: function(data){
				if(data == 1){
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_stop(this,\''+  url  +'\')" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
					$(obj).remove();
					layer.msg('已启用!', {icon: 6,time:1000});
				}else{
					layer.msg('启用失败!',{icon:5,time:1000});
				}
			},
			error:function(data) {
				console.log(data.msg);
			},
		});
	});
}

//批量删除
function deleteAll(url){
	var _list = new Array();
	$('input[name="id"]:checked').each(
          function (i) {
          	_list[i] = $(this).val();
          });
	if(_list.length == 0){
		layer.msg("请选择删除的管理员");
		return false;
	}
  layer.confirm('确定要删除选中的全部数据吗？', {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		 $.ajax({  
	   	     url:url,// 跳转到 action  
	   	     data:JSON.stringify(_list),  
	   	     type:'post',  
	   	     cache:false,  
	   	     dataType:'json', 
	   	     contentType:"application/json",  //发送信息至服务器时内容编码类型。    
	   	     success:function(data) {  
	   	         if(data == 1){  
	   	            window.location.reload();  
	   	         }else{  
	   	        	layer.msg('删除失败!',{icon:5,time:1000});
	   	         }  
	   	      },  
	   	      error : function() {  
	   	    	  layer.msg("异常！");  
	   	      }  
	        });
	}, function() {

	});
}
</script>
</body>
</html>